/*
 * Copyright 2006-2007 S W McIlroy
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockfu.junit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.internal.runners.TestClassMethodsRunner;
import org.junit.internal.runners.TestMethodRunner;
import org.junit.runner.notification.RunNotifier;
import org.mockfu.ExpectationFailure;
import org.mockfu.Injection;
import org.mockfu.MockControl;

/**
 * JUnit 4.3 and earlier implementation.
 */
class FourDotOhVerifier extends TestClassMethodsRunner
{
    public FourDotOhVerifier(Class<?> klass, boolean parallelize)
    {
        super(klass);
    }

    @Override @SuppressWarnings("unchecked")
    protected TestMethodRunner createMethodRunner(Object test, Method method, RunNotifier notifier)
    {
        final MockControl<Verifier>[] verifier = new MockControl[1];

        return new TestMethodRunner(test, method, notifier, methodDescription(method))
        {
            @Override
            public void runProtected()
            {
                try
                {
                    verifier[0] = MockControl.of(Verifier.class);
                    super.runProtected();
                }
                finally
                {
                    Injection.ejectAll();
                }
            }

            @Override
            protected void executeMethodBody() throws IllegalAccessException, InvocationTargetException
            {
                super.executeMethodBody();
                try
                {
                    verifier[0].verifyAll();
                }
                catch (ExpectationFailure ex)
                {
                    throw new InvocationTargetException(ex);
                }
            }
        };
}
}